package com.ljkj.cordial.chat.util;

import android.app.Activity;
import android.text.TextUtils;

import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.dao.AlbumTableBean;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.ljkj.cordial.chat.dao.ChatTableBeanDao;
import com.ljkj.cordial.chat.dao.DbController;
import com.ljkj.cordial.chat.dao.notify.GroupTable;

import java.util.List;

/**
 * 缓存表文件到本地
 */
public class TableFileCacheUtil {
    private static class Build {
        public static TableFileCacheUtil INSTANCE = new TableFileCacheUtil();
    }

    public static TableFileCacheUtil getInstance() {
        return Build.INSTANCE;
    }

    public void downLoadChatFilesToLocTable(Activity activity) {
        if (activity == null || activity.isFinishing()) {
            return;
        }
        List<ChatTableBean> list = DbController.getInstance().getRead().getChatTableBeanDao().queryBuilder()
                .where(ChatTableBeanDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).list();
        if (list != null && !list.isEmpty()) {
            for (ChatTableBean chatTable : list) {
                if (chatTable.getFromType() == Canstants.SINGLE_CHAT
                        && TextUtils.isEmpty(chatTable.getAlbumId()) &&
                        MsgType.Voice != chatTable.getChatType()
                        && !TextUtils.isEmpty(chatTable.getFileUrl())) {
                    int mType = AlbumTableBean.TYPE_IMAGE;
                    if (MsgType.ANNEX == chatTable.getChatType()) {
                        mType = AlbumTableBean.TYPE_EXTRA_FILE;
                    } else if (MsgType.Picture == chatTable.getChatType()) {
                        mType = AlbumTableBean.TYPE_IMAGE;
                    } else if (MsgType.Voice == chatTable.getChatType()) {
                        mType = AlbumTableBean.TYPE_RECORD_FILE;
                    } else if (MsgType.Video == chatTable.getChatType()) {
                        mType = AlbumTableBean.TYPE_VIDEO;
                    }
                    int finalMType = mType;
                    activity.runOnUiThread(() ->
                            AlbumManageUtil.getInstance().downLoadFileFriend(chatTable.getFileUrl(), finalMType, chatTable.getTargetId(), new AlbumManageUtil.OnFileResponse() {
                                @Override
                                public void onResult(String path, String fileId) {
                                    chatTable.setAlbumId(fileId);
                                    DbController.getInstance().getWrite().update(chatTable);
                                }
                            }));

                } else {
                    if (chatTable.getFromType() == Canstants.GROUP_CHAT
                            && TextUtils.isEmpty(chatTable.getAlbumId()) &&
                            MsgType.Voice != chatTable.getChatType()
                            && !TextUtils.isEmpty(chatTable.getFileUrl())) {
                        int mType = AlbumTableBean.TYPE_IMAGE;
                        if (MsgType.ANNEX == chatTable.getChatType()) {
                            mType = AlbumTableBean.TYPE_EXTRA_FILE;
                        } else if (MsgType.Picture == chatTable.getChatType()) {
                            mType = AlbumTableBean.TYPE_IMAGE;
                        } else if (MsgType.Voice == chatTable.getChatType()) {
                            mType = AlbumTableBean.TYPE_RECORD_FILE;
                        } else if (MsgType.Video == chatTable.getChatType()) {
                            mType = AlbumTableBean.TYPE_VIDEO;
                        }
                        int finalMType = mType;
                        activity.runOnUiThread(() ->
                                AlbumManageUtil.getInstance().downLoadFileGroupMember(chatTable.getFileUrl(), finalMType,chatTable.getGroupType(), chatTable.getGroupId(),chatTable.getTargetType(), chatTable.getTargetId(), new AlbumManageUtil.OnFileResponse() {
                                    @Override
                                    public void onResult(String path, String fileId) {
                                        chatTable.setAlbumId(fileId);
                                        DbController.getInstance().getWrite().update(chatTable);
                                    }
                                }));
                    }
                }
            }
        }
    }

}
